<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Sales extends CI_Controller {

    public function index() {
        
    }

    public function search($parametro) {
        $this->load->model('Document_types_model');
        $this->load->model('Clients_model');
        $vars['typeDocuments'] = $this->Document_types_model->getAll();
        $vars['clients'] = $this->Clients_model->getAll();
        if (isset($parametro) == 1) {
            $vars['parametro'] = 1;
        }
        $vars['content_view'] = 'sales_form';
        $vars['fecha'] =  date('d/m/Y');
        $vars['menu'] = 'sales';
        $this->load->view('template', $vars);
    }

    public function add($success_message = null) {
        $this->load->model('Document_types_model');
        $this->load->model('Clients_model');
        $vars['typeDocuments'] = $this->Document_types_model->getAll();
        $vars['clients'] = $this->Clients_model->getAll();
        if (isset($success_message)) {
            $vars['success_message'] = true;
        }
        $vars['content_view'] = 'sales_form';
        $vars['menu'] = 'sales';
        $this->load->view('template', $vars);
    }

    public function client() {
        $idClient = $this->input->post('id');
        $this->load->model('Clients_model');
        $dataClient = $this->Clients_model->find($idClient);
        echo json_encode($dataClient);
    }

    public function products() {
        $code = null;
        $description = null;
        $this->load->model('Products_model');
        if ($this->input->post('code')) {
            $code = $this->input->post('code');
        }
        if ($this->input->post('description')) {
            $description = $this->input->post('description');
        }
        $products = array();
        if ($code != null || $description != null) {
            $products = $this->Products_model->search($code, $description);
        }
        $vars['products'] = $products;
        $vars['code'] = $code;
        $vars['description'] = $description;
        $this->load->view('products_list', $vars);
    }

    public function stocks() {
        $code = null;
        $description = null;
        if ($this->input->post('code')) {
            $code = $this->input->post('code');
        }
        if ($this->input->post('description')) {
            $description = $this->input->post('description');
        }
        $this->load->model('Warehouses_product_model');
        $this->load->model('Products_model');
        $listStock = $this->Warehouses_product_model->listStockByProduct($code);
        $productData = $this->Products_model->find($code);
        $vars['priceProduct'] = $productData->price;
        $vars['codeProduct'] = $code;
        $vars['descriptionProduct'] = $description;
        $vars['listStock'] = $listStock;
        $vars['productData'] = $productData;
        $this->load->view('stock_list', $vars);
    }

    public function submit() {
        $this->load->model('Sales_model');
        $this->load->model('Sales_details_model');
        $dateRegister = explode('/', $_POST['date']);
        $this->Sales_model->addPurchase(
                array(
                    'document_type_id' => $_POST['tipo'],
                    'document_code' => $_POST['numero'],
                    'date' => date('Y-m-d H:i:s', strtotime($dateRegister[2] . '/' . $dateRegister[1] . '/' . $dateRegister[0])),
                    'id_client' => $_POST['client'],
                    'observations' => $_POST['observations'],
                    'amount' => $_POST['amount']
                )
        );
        $idPurchase = $this->db->insert_id();
        if (is_array($_POST['product']) && count($_POST['product']) > 0) {
            foreach ($_POST['product'] as $codeProduct => $stockData) {
                foreach ($stockData as $idStock => $mount) {
                    $this->Sales_details_model->addPurchaseDetails(
                            array(
                                'product_code' => $codeProduct,
                                'id_warehouse' => $idStock,
                                'id_sale' => $idPurchase,
                                'cant' => $mount
                            )
                    );
                }
            }
        }
        $this->add(true);
    }

}